package com.gitee.hljdrl.clip;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;

import com.gitee.hljdrl.clip.basis.CornersEvent;
import com.gitee.hljdrl.clip.basis.ShapeCorners;

public class ClipConstraintLayout extends androidx.constraintlayout.widget.ConstraintLayout {

    private ShapeCorners mShapeCorners;

    public ClipConstraintLayout(Context context) {
        this(context, null);
    }

    public ClipConstraintLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClipConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mShapeCorners = new ShapeCorners();
        mShapeCorners.init(context, attrs, this);
    }

    public CornersEvent getCornersEvent() {
        return mShapeCorners;
    }

    @Override
    public void draw(Canvas canvas) {
        if (getBackground() != null) {
            if (mShapeCorners != null) {
                mShapeCorners.onClipDraw(canvas);
            }
        }
        super.draw(canvas);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        if (mShapeCorners != null) {
            mShapeCorners.onClipDraw(canvas);
        }
        //要在调用super.dispatchDraw之前设置裁切范围噢
        super.dispatchDraw(canvas);
    }
}
